package net.unix8.thread;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.unix8.constant.ConstantsValue;
import net.unix8.server.ServerHandler;
import net.unix8.share.NetworkShare;
import net.unix8.tool.FileProcess;
import net.unix8.tool.Tools;

//System.out.println("fileName>"+ PathKit.getWebRootPath() + "\\data");

public class DownloadLoopHost  extends Thread {
	private volatile  boolean suspend = false;  
	private String control = ""; // 只是需要一个对象而已，这个对象没有实际意义  

	public void setSuspend(boolean suspend) {  
		if (!suspend) {  
			synchronized (control) {  
				control.notifyAll();  
			}  
		}  
		this.suspend = suspend;  
	}  
	private boolean exit = false;
	public boolean isSuspend() {  
		return this.suspend;  
	}  

	public boolean isExit() {
		return exit;
	}
	public void setExit(boolean exit) {
		this.exit = exit;
	}

	@Override 
	public void run() { 
		/*
		try {
			FileProcess.timeModifyNoSQL("192.168.127.171");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(true)return;
		*/
		
		Date dt = new Date();
		SimpleDateFormat matter1=new SimpleDateFormat("yyyyMMddHHmmss");
		NetworkShare.startTime = matter1.format(dt);
		
		System.out.println(NetworkShare.startTime+"开始处理遗留文件,请稍等\n");
		FileProcess.checkLeaveHostFile();
		System.out.println("遗留文件处理结束\n");
		String hostString; 
		while (true) {  
			if(ServerHandler.hostListALL.size() == 0) {
				Tools.sleep(3000);
			}
			//System.out.println("线程数"+ServerHandler.workingHostsNum());
			for (int i = 0; i < ServerHandler.hostListALL.size();) { 
				hostString =  ServerHandler.hostListALL.get(i);//第i个站的站名				
				if(ServerHandler.isInWorking(hostString)){//如果这个站正在下载，不管长下载还是短下载
					i++;
					Tools.sleep(1000);
				}else{
					if((ServerHandler.workingHostsNum() < ConstantsValue.THREADS_IN_MAP)){					
						i++;
						if(ServerHandler.addWorking(hostString, true)){	
							if(hostString.equals(NetworkShare.paodianHost)) {//如果是炮点文件								
								Thread pdThread = new DownloadPointFile(hostString);
								NetworkShare.taskPool.execute(pdThread);		
							}else {
							//System.out.println("新建线程..."+ServerHandler.workingHostsNum());
								Thread workThread = new ContinuousDownloadFtp(hostString);
								NetworkShare.taskPool.execute(workThread);
							}
						}
					}else{
						Tools.sleep(1000);
					}
					
				}
			}
		}
	}  

}